home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / cxref-1.001 / cxref-1~ / cxref / preproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-24  |  4.6 KB  |  201 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/preproc.c 1.6 1996/02/24 14:53:40 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.0
  5.  
  6.   Collects the pre-processing instruction stuff.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Control the output of debugging information for this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "datatype.h"
  25. #include "cxref.h"
  26.  
  27. /*+ The file that is currently being processed. +*/
  28. extern File CurFile;
  29.  
  30. /*+ When in a header file, this is set to 1, to allow most of the stuff to be skipped. +*/
  31. int in_header=0;
  32.  
  33. /*+ The current #define we are looking at. +*/
  34. static Define cur_def=NULL;
  35.  
  36. /*+ The depth of the include hierarchy etc. +*/
  37. static int inc_depth=0,in_sys_inc=0;
  38.  
  39. /*++++++++++++++++++++++++++++++++++++++
  40.   Function that is called when a included file is seen in the current file.
  41.  
  42.   char* name The name of the included file.
  43.  
  44.   int flag The flags that GCC leaves in the file
  45.   ++++++++++++++++++++++++++++++++++++++*/
  46.  
  47. void SeenInclude(char* name,int flag)
  48. {
  49.  Include inc;
  50.  
  51.  if(name[0]=='/') flag|=8;
  52.  
  53. #if DEBUG
  54.  printf("#Preproc.c# #include %s '%s' (flag=%d)\n",flag&2?"Included":"Return to",name,flag);
  55. #endif
  56.  
  57.  if(flag & 2)
  58.    {
  59.     if(!(flag&8) || !in_sys_inc)
  60.       {
  61.        Include *t=&CurFile->includes;
  62.        int i;
  63.  
  64.        for(i=0;i<inc_depth;i++)
  65.          {
  66.           while(*t && (*t)->next)
  67.              t=&(*t)->next;
  68.           t=&(*t)->includes;
  69.          }
  70.  
  71.        inc=(Include)Malloc(sizeof(struct _Include));
  72.  
  73.        AddToLinkedList(*t,Include,inc);
  74.  
  75.        inc->comment=MallocString(GetCurrentComment());
  76.        inc->name=MallocString(name);
  77.        inc->scope=(flag&8)?GLOBAL:LOCAL;
  78.        inc->includes=NULL;
  79.        inc->next=NULL;
  80.       }
  81.  
  82.     if(!(flag&8))
  83.       {
  84.        in_sys_inc=0;
  85.        inc_depth++;
  86.       }
  87.     else
  88.        in_sys_inc=1;
  89.    }
  90.  else
  91.     if(!(flag&8))
  92.       {
  93.        if(in_sys_inc)
  94.           in_sys_inc=0;
  95.        else
  96.           inc_depth--;
  97.       }
  98.  
  99.  if(inc_depth==0 && !in_sys_inc)
  100.     in_header=0;
  101.  else
  102.     if(!in_sys_inc)
  103.        in_header=LOCAL;
  104.     else
  105.        in_header=GLOBAL;
  106. }
  107.  
  108.  
  109. /*++++++++++++++++++++++++++++++++++++++
  110.   Function that is called when a #define is seen in the current file.
  111.  
  112.   char* name The name of the #defined symbol.
  113.   ++++++++++++++++++++++++++++++++++++++*/
  114.  
  115. void SeenDefine(char* name)
  116. {
  117.  Define def;
  118.  
  119. #if DEBUG
  120.  printf("#Preproc.c# Defined name '%s'\n",name);
  121. #endif
  122.  
  123.  def=(Define)Malloc(sizeof(struct _Define));
  124.  
  125.  AddToLinkedList(CurFile->defines,Define,def);
  126.  
  127.  def->comment=MallocString(GetCurrentComment());
  128.  def->name=MallocString(name);
  129.  def->value=NULL;
  130.  InitStringList2(&def->args);
  131.  def->next=NULL;
  132.  
  133.  cur_def=def;
  134. }
  135.  
  136.  
  137. /*++++++++++++++++++++++++++++++++++++++
  138.   Function that is called when a #define value is seen in the current file.
  139.  
  140.   char* value The value of the #defined symbol.
  141.   ++++++++++++++++++++++++++++++++++++++*/
  142.  
  143. void SeenDefineValue(char* value)
  144. {
  145. #if DEBUG
  146.  printf("#Preproc.c# #define value '%s' for %s\n",value,cur_def->name);
  147. #endif
  148.  
  149.  cur_def->value=MallocString(value);
  150. }
  151.  
  152.  
  153. /*++++++++++++++++++++++++++++++++++++++
  154.   Function that is called when a #define function argument is seen in the current definition.
  155.  
  156.   char* name The argument.
  157.   ++++++++++++++++++++++++++++++++++++++*/
  158.  
  159. void SeenDefineFunctionArg(char* name)
  160. {
  161. #if DEBUG
  162.  printf("#Preproc.c# #define Function arg '%s' in %s()\n",name,cur_def->name);
  163. #endif
  164.  
  165.  AddToStringList2(&cur_def->args,MallocString(name),SplitComment(&cur_def->comment,name));
  166. }
  167.  
  168.  
  169. /*++++++++++++++++++++++++++++++++++++++
  170.   Function that is called when a comment is seen in a #define function definition.
  171.   ++++++++++++++++++++++++++++++++++++++*/
  172.  
  173. void SeenDefineFuncArgComment(void)
  174. {
  175.  char* comment=GetCurrentComment();
  176.  
  177. #if DEBUG
  178.  printf("#Preproc.c# #define Function arg comment '%s' in %s()\n",comment,cur_def->name);
  179. #endif
  180.  
  181.  if(!cur_def->args.s2[cur_def->args.n-1])
  182.     cur_def->args.s2[cur_def->args.n-1]=MallocString(comment);
  183. }
  184.  
  185.  
  186. /*++++++++++++++++++++++++++++++++++++++
  187.   Function that is called when a comment is seen in a #define definition.
  188.   ++++++++++++++++++++++++++++++++++++++*/
  189.  
  190. void SeenDefineComment(void)
  191. {
  192.  char* comment=GetCurrentComment();
  193.  
  194. #if DEBUG
  195.  printf("#Preproc.c# #define inline comment '%s' in %s\n",comment,cur_def->name);
  196. #endif
  197.  
  198.  if(!cur_def->comment)
  199.     cur_def->comment=MallocString(comment);
  200. }
  201.